#!/usr/bin/env node
//const eventemitter = require('events').eventemitter;
const {EventEmitter} = require('events');
class Dog extends EventEmitter{
  name = '';
  energy = 0;
  timer = null;
  constructor(name,energy){
    super();
    this.name = name;
    this.energy = energy;
    this.startTimer();
  }
  getName(){
    return this.name;
  }
  getEnergy(){
    return this.energy;
  } 
  setEnergy(e){ 
    this.energy = e;
    this.startTimer();
  }
  startTimer(){
    if(this.timer != null) return;
    this.timer = setInterval(()=>{
      if(this.energy > 0){       
        this.energy --;
        this.emit('bark');
      }else{
        clearInterval(this.timer);
        this.timer = null;
      }
    },1000);
  }
}
module.exports = Dog;
